cmake_minimum_required(VERSION 3.18)
project(audio_denoiser)

# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置编译器
if(APPLE)
    # 使用 Homebrew 的 LLVM/Clang
    set(CMAKE_C_COMPILER "/opt/homebrew/opt/llvm/bin/clang")
    set(CMAKE_CXX_COMPILER "/opt/homebrew/opt/llvm/bin/clang++")
    
    # 设置 OpenMP 标志
    set(OpenMP_C_FLAGS "-fopenmp")
    set(OpenMP_CXX_FLAGS "-fopenmp")
    set(OpenMP_C_LIB_NAMES "omp")
    set(OpenMP_CXX_LIB_NAMES "omp")
    set(OpenMP_omp_LIBRARY "/opt/homebrew/opt/libomp/lib/libomp.dylib")
endif()

# 添加 OpenMP 支持
find_package(OpenMP REQUIRED)

# 添加 Torch 的 cmake 路径
list(APPEND CMAKE_PREFIX_PATH "${CMAKE_CURRENT_SOURCE_DIR}/libtorch")

# 设置 RPATH
set(CMAKE_INSTALL_RPATH "${CMAKE_CURRENT_SOURCE_DIR}/libtorch/lib")
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

# 添加 Homebrew OpenMP 路径到 RPATH
list(APPEND CMAKE_INSTALL_RPATH "/opt/homebrew/opt/libomp/lib")

# 查找必需的包
find_package(Torch REQUIRED)
find_package(SndFile REQUIRED)  # 音频处理库

# 添加源文件
add_executable(denoiser
    src/main.cpp
    src/audio_utils.cpp
    src/denoiser.cpp
    src/model.cpp
)

# 包含头文件目录
target_include_directories(denoiser PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    /opt/homebrew/opt/libomp/include  # 添加 OpenMP 头文件路径
)

# 链接库
target_link_libraries(denoiser PRIVATE 
    ${TORCH_LIBRARIES}
    SndFile::sndfile
    OpenMP::OpenMP_CXX
)

# 设置编译选项
if(MSVC)
    target_compile_options(denoiser PRIVATE /W4)
else()
    target_compile_options(denoiser PRIVATE -Wall -Wextra)
endif()

# 复制 LibTorch DLL (仅 Windows)
if(MSVC)
    file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
    add_custom_command(TARGET denoiser
        POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
        ${TORCH_DLLS}
        $<TARGET_FILE_DIR:denoiser>
    )
endif() 